package com.first.orange.controller;

import com.first.orange.beans.UserLogin;
import com.first.orange.service.UserService;
import com.first.orange.util.EncodeUtils;
import com.first.orange.util.SendMessageUtil;
import com.first.orange.util.SendSmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Random;


/**
 * Created on 2021/7/5.
 * <p>
 * Author: wanglinjun
 * <p>
 * Description:
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 跳转登录页面
     *
     * @return
     */
    @RequestMapping(value = "/doLogin")
    public String doLogin(Map<String, Object> map, HttpServletRequest request) {
        return "home/login";
    }

    /**
     * 通过username在数据库中查询password与前端参数做对比校验
     *
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(Map<String, Object> map, HttpServletRequest request, HttpSession session) {
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        map.put("userName", userName);
        map.put("password", password);
        String md5Password = EncodeUtils.testDigest(password);
        // 调用业务层方法根据username获取登录用户实体
        if (userService.queryshowList(userName, md5Password)) {
            session.setAttribute("user", userService.queryUser(userName, md5Password));
            map.put("loginMsg", "");
            return "redirect:/home";
        } else {
            map.put("loginMsg", "账户或密码错误，请重试！");
            return "home/login";
        }
    }

    /**
     * 跳转注册页面
     *
     * @return
     */
    @RequestMapping(value = "/register")
    public String register() {
        return "home/register";
    }

    /**
     * 用户注册
     *
     * @param map
     * @param userLogin
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(Map<String, Object> map, UserLogin userLogin, HttpSession httpSession, String code, HttpServletRequest request) {
        SendMessageUtil sendMessageUtil = new SendMessageUtil();
        map.put("userLogin", userLogin);

        //判断验证码是否超时
        if (!sendMessageUtil.checkMessage(httpSession)) {
            map.put("registerMsg", "时间超过5min，请重新发送验证码。");
            return "home/register";
        }
        //判断验证码是否正确
        if (!code.equals(httpSession.getAttribute("code"))) {
            map.put("registerMsg", "验证码输入错误！请重新输入。");
            return "home/register";
        }
        // 判断两次密码输入是否一致
        String password = request.getParameter("password");
        String password1 = request.getParameter("password1");
        if (!password.equals(password1)) {
            map.put("registerMsg", "两次密码输入不一致！请重新输入。");
            return "home/register";
        }
        // 调用业务层方法根据 用户名或手机号码 判断该用户是否存在，若存在执行注册
        // 调用业务层方法，判断该用户名是否已经注册
        if (userService.userRegister(userLogin)) {
            map.put("registerMsg", "注册失败！该用户名已被注册。");
            return "home/register";
        }
        // 调用业务层方法，判断该手机号是否已经注册
        if (userService.userRegisterByPhone(userLogin)) {
            map.put("registerMsg", "注册失败！该手机号已被注册。");
            return "home/register";
        }
        //全部判断成功，注册用户
        String md5Password = EncodeUtils.testDigest(userLogin.getPassword());
        userLogin.setPassword(md5Password);
        userService.doRegister(userLogin);
        map.put("registerMsg", "注册成功！");
        return "redirect:/doLogin";
    }

    /**
     * 生成验证码
     *
     * @param httpSession
     * @return
     */
    @RequestMapping(value = "/sendMobileCode")
    @ResponseBody
    public String sendMessage(HttpSession httpSession, HttpServletRequest request) {
        // 生成随机数
        String code = "";
        Random random = new Random();
        code = random.nextInt(900000) + 100000 + "";

        //获取手机号
        String phone = request.getParameter("phone");

        System.out.println("************" + code);

        //发送验证码
        SendSmsUtil.sendSms(phone, code);

        httpSession.setAttribute("code", String.valueOf(code));
        httpSession.setAttribute("start", System.currentTimeMillis());
        return "code";
    }

}
